var express = require('express');

var app = express();

var multiparty = require('multiparty');
var fs = require('fs');

app.use(express.static(__dirname));
var port = 80;
var control = require("./route/control.js");
app.listen(port,function(){
	console.log(port);
});

//图片   待定
/*app.post('/loadimage', function(req, res,next){
	var form = new multiparty.Form({uploadDir: './images'});
	form.parse(req, function (err, fileds, files) {
		var filesTmp = JSON.stringify(files, null, 2);
		if(err) console.log(err);
		else{
		var inputFile =files.file[0];
		var uploadedPath = inputFile.path;
		var dstPath = './images/' + inputFile.originalFilename;
		//重命名为真实文件名
		fs.rename(uploadedPath, dstPath, function (err) {
			if (err) {
				console.log('rename error: ' + err);
			} else {
				console.log(dstPath);
				console.log('rename ok');
				res.send(console.log(dstPath[0].path));
			}
		});}
	})
});*/

app.post('/form_file',function(req,res){
	var form = new multiparty.Form({uploadDir:'./images/'});
	form.parse(req,function(err,filed,files){
		if(err){console.log(err)}
		else{
			//console.log(files);
			var old_path =  files.file[0].path,
				new_path = files.file[0].originalFilename;
			console.log(old_path,new_path);
			fs.rename(old_path,'./images/'+new_path,function(err){
				if(err){throw err}
				else{
					console.log("图片路径"+old_path+new_path);
					console.log(files);
					res.json({msg:""+new_path})
				}
			})
		}
	})
});


//图片
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));

app.get("/",function(request,response){
	response.write("<h1>讲的是卡</h1>");
	response.json("Hello");
});


//移动端


//评论——qp
app.post("/posttalk",control.posttalk);
//注册——查询——qp
app.post("/postseachuser",control.postseachuser);
//注册——添加新用户——qp
app.post("/postadduser",control.postadduser);
//登录——qp
app.post("/postloginuser",control.postloginuser);
//刷新页面——qp
app.post("/postupdata",control.postupdata);

//0518
app.post("/uploadUserinfo",control.uploadUserinfo);
//PC端


//注册——查询——qp
app.post("/postseachuser_PC",control.postseachuser_PC);
//注册——添加新用户——qp
app.post("/postadduser_PC",control.postadduser_PC);
//登录——qp
app.post("/postloginuser_PC",control.postloginuser_PC);
//评论——qp
app.post("/posttalk_PC",control.posttalk_PC);
//刷新页面——qp
app.post("/postupdata_PC",control.postupdata_PC);
//点赞——qp
app.post("/postdianzan",control.postdianzan);



//李梦辉
app.post('/spotSee',control.spotSee);

//face userinfo 头像
/*
app.post('/file', function(req, res,next){
	var form = new multiparty.Form({uploadDir: './images'});
	form.parse(req,function(error,fileds,files){
		var filesTmp = JSON.stringify(files,null,2);
		console.log(files);
		var inputFile = files.file[0];
		var uploadPath = inputFile.path;
		var dstPath = './images'+inputFile.originalFilename;
		fs.rename(uploadedPath, dstPath, function (error) {
			if (error) {
				console.log('rename error: ' + error);
			} else {
				console.log(dstPath);
				console.log('rename ok');
			}
		});
	})
});
*/
